package org.apache.servicecomb.samples.practise.houserush.customer.manage.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@Entity
@Table(name = "customers")
@EntityListeners(AuditingEntityListener.class)
//活动使用逻辑删除
@SQLDelete(sql = "update customers set deleted_at = now() where id = ?")
@Where(clause = "deleted_at is null")
public class Customer {
    @Id
    private Integer id;
    private String phone;
    private String name;
    private String idCard;
    private String realName;
    private String address;
    @Temporal(TemporalType.TIMESTAMP)
    private Date deletedAt;
    @Temporal(TemporalType.TIMESTAMP)
    @CreatedDate
    private Date createdAt;
    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    private Date updatedAt;

    @OneToMany(mappedBy = "customer",cascade = {CascadeType.PERSIST,CascadeType.MERGE},
    orphanRemoval = true)
    @JsonIgnoreProperties(ignoreUnknown = true, value = {"customer"})
    private List<Qualification> qualifications = new ArrayList<>();
}
